-- 呼吸写的,冰汽爬

-- only 呼吸
-- 这么写多简单, 之前非得一个个测试
local SHOW_RANGE_TIME = 30
local rangeTable = require("QAQ/rangetable")
local C = rangeTable.click_range
local H = rangeTable.hover_range
local P = rangeTable.placer_range
local t_util = require "libs/tableutil"

-- click 画圆
local controller = GLOBAL.require "components/playercontroller"
local _OnLeftClick = controller.OnLeftClick
function controller:OnLeftClick(down, ...)
	if InGame() and not down then
		local item = GLOBAL.TheInput:GetWorldEntityUnderMouse()
		if item and item:IsValid() then
			local prefab = item.prefab
			local info = C[prefab]
			if info then
				local color = info[1] or "white"
				t_util:Pairs(info, function(k, range)
					if type(k) == "number" and k>=2 and type(range) == "number" then
						local circle = GLOBAL.SpawnPrefab("hrange"):setColor(color):setParentScale(item, range)
						circle:DoTaskInTime(SHOW_RANGE_TIME, circle.Remove)
					end
				end)
			end
			-- 妈的，一个tag还藏着掖着
			if prefab == "spiderden" and item:HasTag("bedazzled") and GLOBAL.TUNING then
				local radius = GLOBAL.TUNING.BEDAZZLEMENT_RADIUS
				local range
				if radius then
					if SearchForAnim({"cocoon_medium"}, item) then
						range = radius[2]
					elseif SearchForAnim({"cocoon_small"}, item) then
						range = radius[1]
					elseif SearchForAnim({"cocoon_large"}, item) then
						range = radius[3]
					end
				end
				if range then
					local circle = GLOBAL.SpawnPrefab("hrange"):setColor("grass"):setParentScale(item, range)
					circle:DoTaskInTime(SHOW_RANGE_TIME, circle.Remove)
				end
			end
		end
	end
	return _OnLeftClick(self,down,...)
end


-- hover 画圆
local circle
local function removeCircle()
	if circle and circle:IsValid() then
		circle:Remove()
	end
	circle = nil
end
AddHovererFuncAndIsOut(function(target, prefab, bind)
	if not circle and type(H[prefab]) == "number" then
		circle = GLOBAL.SpawnPrefab("hrange"):setParentScale(bind, H[prefab]):setColor("blue")
		AddHovererFuncAndIsOut(function()
			removeCircle()
			RemoveHovererFuncAndIsOut(true, "_huxi_draw_circle")
		end, true, "_huxi_draw_circle")
	end
end)

-- placer画圆
-- 该功能使用时需要关闭几何
for place,info in pairs(P)do
	local color = info[1] or "green"
	local range = info[2]

	if range then
		AddPrefabPostInit(place, function(inst)
			inst:DoTaskInTime(0.1, function()
				GLOBAL.SpawnPrefab("hrange"):setParentScale(inst, range):setColor(color)
			end)
		end)
	end
end

-- -- placer画圆,顺便改变颜色
-- AddComponentPostInit("placer", function(self)
-- 	local _oncanbuild = self.oncanbuild
-- 	local _oncannotbuild = self.oncannotbuild

-- 	self.oncanbuild = function(inst, ...)
-- 		return _oncanbuild and _oncanbuild(inst, ...)
-- 	end

-- 	self.oncannotbuild = function(inst, ...)
-- 		return _oncannotbuild and _oncannotbuild(inst, ...)
-- 	end
-- end)